﻿CmnDialog = function () { }

CmnDialog.prototype.show = function (userControlFrameDom, title, hasCoverDiv) {
    var border = "";    
    var id = userControlFrameDom.id;
    var width = userControlFrameDom.style.width;
    var height = userControlFrameDom.style.height;

    if (hasCoverDiv) {
        if ($j("#cmnDialogCover_" + id).length == 0) {
            border = "<div id='cmnDialogCover_" + id + "' style='background:#F0F0F0; filter: alpha(opacity=45); opacity:0.3; position:absolute;top:0px;left:0px; width:100%; height:100%;z-index:1'>";
            $j("body").append(border);
        }
        else {
            $j("#cmnDialogCover_" + id).css("display", "");

        }
    }

    if ($j("#cmnDialog_" + id).length == 0) {
        border = "<div id='cmnDialog_" + id + "' style='position: absolute; display:none;'>";
        border += "  <table border='0' cellpadding='0' cellspacing='0'>";
        border += "    <tr id='cmnDialogTitle_" + id + "' style='cursor: move;'>";
        border += "      <td width='13' height='33' style='background-image: url(../../Content/images/cmnMessage/dialog_lt.png);'></td>";
        border += "      <td width='" + width + "'height='33' style='background-image:url(../../Content/images/cmnMessage/dialog_ct.png);font-weight:bolder;font-size:14px;'><div style='margin-left:20px;margin-top:10px;color:black;'>" + title + "</div></td>";
        border += "      <td width='13' height='33' style='background-image: url(../../Content/images/cmnMessage/dialog_rt.png);'></td>";
        border += "    </tr>";
        border += "    <tr>";
        border += "      <td width='13' height='" + height + "' style='background-image: url(../../Content/images/cmnMessage/dialog_mlb.png);'></td>";
        border += "      <td valign='top'><div id='cmnDialogMain_" + id + "'></div></td>";
        border += "      <td width='13' style='background-image: url(../../Content/images/cmnMessage/dialog_mrb.png);'></td>";
        border += "    </tr>";
        border += "    <tr>";
        border += "      <td width='13' height='13' style='background-image: url(../../Content/images/cmnMessage/dialog_lb.png);'></td>";
        border += "      <td height='13' style='background-image: url(../../Content/images/cmnMessage/dialog_cb.png);'></td>";
        border += "      <td width='13' height='13' style='background-image: url(../../Content/images/cmnMessage/dialog_rb.png);'></td>";
        border += "    </tr>";
        border += "  </table>";
        border += "</div>";

        $j("body").append(border);

        $j("#" + id).appendTo("#cmnDialogMain_" + id);

        $j("#cmnDialog_" + id).css("z-index", 2);
    }


    $j("#cmnDialog_" + id).animate({ opacity: "show", left: ($j(window).width() - $j("#cmnDialog_" + id).width()) / 2, top: ($j(window).height() - $j("#cmnDialog_" + id).height()) / 2 }, 500);

    Event.observe($("cmnDialogTitle_" + id), 'mousedown', function (event) { CmnDialog.DialogMove($("cmnDialog_" + id).id, event) } .bind(this));

}

CmnDialog.prototype.hide = function (userControlFrameDom) {
    var id = userControlFrameDom.id;

    $j("#cmnDialog_" + id).animate({ opacity: "hide", left: 0, top: 0 }, 500);

    $j("#cmnDialogCover_" + id).css("display", "none");
}

CmnDialog.prototype.DialogMove = function (containerID, event) {
    if (!Event.isLeftClick(event)) {
        return;
    }

    var controlContainerObj = $(containerID);


    var maxLeft = parseInt($j("#" + containerID).parent().css("width"), 10) - parseInt(controlContainerObj.clientWidth, 10);

    var left = controlContainerObj.style.left;
    var top = controlContainerObj.style.top;

    var x = parseInt(left, 10);
    var y = parseInt(top, 10);

    var mouseLeft = event.x;
    var mouseTop = event.y;

    Event.observe(document, 'mousemove', MouseMoveHandler);
    Event.observe(document, 'mouseup', MouseUpHandler);
    Event.observe(document, "selectstart", SelectStartHandler);

    Event.stop(event);

    function MouseMoveHandler(event) {
        var moveLeft = event.x - mouseLeft + x;

        if (moveLeft >= 0) {
            if (moveLeft > maxLeft) {
                controlContainerObj.style.left = maxLeft + "px";
            }
            else {
                controlContainerObj.style.left = moveLeft + "px";
            }
        }
        else {
            controlContainerObj.style.left = "0px";
        }

        var moveTop = event.y - mouseTop + y;

        if (moveTop >= 0) {
            controlContainerObj.style.top = moveTop + "px";
        }
        else {
            controlContainerObj.style.top = "0px";
        }

        SelectStartHandler();
        Event.stop(event);
    }

    function MouseUpHandler(event) {
        Event.stopObserving(document, 'mousemove', MouseMoveHandler);
        Event.stopObserving(document, 'mouseup', MouseUpHandler);
        Event.stopObserving(document, "selectstart", SelectStartHandler);
        Event.stop(event);
    }

    function SelectStartHandler(e) {
        return false;
    }
}



CmnDialog = new CmnDialog();